//
//  GitInfo.m
//  iLiteNote
//
//  Created by xiangbc on 16/3/21.
//  Copyright © 2016年 LiteNice. All rights reserved.
//

#import "GitInfo.h"

@interface GitInfo() {
}

@end


NSString* getDataPath() {
    NSString *path = [NSString stringWithFormat:@"%@/Documents/data.dat", NSHomeDirectory()];
    return path;
}

@implementation GitInfo

+ (GitInfo*)shareGitInfo {
    static GitInfo *singleton = nil;
    /*static dispatch_once_t once;
    dispatch_once(&once, ^{
        singleton = [NSKeyedUnarchiver unarchiveObjectWithFile:getDataPath()];
    });*/
    if (!singleton) {
        
        NSString *path = getDataPath();
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path]) {
            singleton = [NSKeyedUnarchiver unarchiveObjectWithFile:getDataPath()];
        } else {
            singleton = [[GitInfo alloc] init];
        }
    }
    return singleton;
}

/*- (void)setGitURL:(NSString *)gitURL {
    self.gitURL = gitURL;
}*/

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    
    if (self) {
        self.gitURL = [aDecoder decodeObjectForKey:@"gitURL"];
        self.account = [aDecoder decodeObjectForKey:@"account"];
        self.password = [aDecoder decodeObjectForKey:@"password"];
        self.locatePath = [aDecoder decodeObjectForKey:@"locatePath"];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.gitURL forKey:@"gitURL"];
    [aCoder encodeObject:self.account forKey:@"account"];
    [aCoder encodeObject:self.password forKey:@"password"];
    [aCoder encodeObject:self.locatePath forKey:@"locatePath"];
}

- (NSString*) getGitPath {
    return [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), self.locatePath];
}

- (void)WriteGitInfo{
    [NSKeyedArchiver archiveRootObject:self toFile:getDataPath()];
}
@end

